$(function () {
    getAllOrder();

    $("#addReceiverBtn").click(function () {
        getPage($("#addReceiverPage")[0], ctx + '/user/addReceiverPage.do');
    });

});

function getAllOrder() {
    $('#manageOrderList>li').each(function (index) {
        if (index > 1)
            $(this).remove();
        if (index == 1)
            $(this).show();
    });
    $.ajax({
        url: ctx + "/order/listForShop.do",
        data: {},
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                var operation = '<a class="viewOrder del_item block" href="'+ctx+'/order/orderDetail.do?id='+data[i].id+'">查看详情</a>';
                switch (data[i].status) {
                    case "0":
                        data[i].status = "已下单";
                        operation += '<a class="viewOrder del_item block deliverGood" href="'+ctx+'/order/orderDetail.do?deliver=1&id='+data[i].id+'">发货</a>';
                        operation += '<a class="del_item red_txt block cancelOrder" onclick="cancelOrder(' + data[i].id + ',this)">取消订单</a>';
                        break;
                    case "1":
                        data[i].status = "已发货";
                        operation += '<a class="del_item red_txt block cancelOrder" onclick="cancelOrder(' + data[i].id + ',this)">取消订单</a>';
                        break;
                    case "2":
                        data[i].status = "已收货";
                        break;
                    case "3":
                        data[i].status = "买家已取消订单";
                        break;
                    case "4":
                        data[i].status = "店家已取消订单";
                        break;
                }
                $('#manageOrderList').append('<li class="list_li c_txt">' +
                    '<div class="list_item_a">' + data[i].code + '</div>' +
                    '<div class="list_item_e" style="padding-left: 10px;padding-right: 10px;">' + data[i].receiverName + '</div>' +
                    '<div class="list_item_d">' + data[i].placeTime + '</div>' +
                    '<div class="list_item_d orderStatus">' + data[i].status + '</div>' +
                    '<div class="list_item_d">￥' + data[i].sum + '</div>' +
                    '<div class="list_item_c">' + data[i].shopName + '</div>' +
                    '<div class="list_item_f">' +
                    operation +
                    '</div>' +
                    '<div class="clear"></div>' +
                    '</li>');
            }
            $('#manageOrderList>li:eq(1)').hide();
        }
    })

}

function cancelOrder(orderId,me) {
    prompt("取消原因", "", "cancelReason", function (result) {
        if (result == "yes") {
            if ($('#cancelReason').val() == "") {
                alert("请填写取消原因");
            } else
                $.ajax({
                    url: ctx + "/order/cancelOrder.do",
                    data: {id: orderId, from: "seller", reason: $('#cancelReason').val()},
                    success: function (data) {
                        if (data.flag > 0){
                            alert("成功取消订单");
                            $(me).siblings(".deliverGood").remove();
                            $(me).parent().siblings('.orderStatus').html('卖家已取消订单');
                            $(me).remove();
                        }
                    }
                });
        }
    });
}